package cn.eoe.example.recordprogressbutton;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import cn.eoe.example.recordprogressbutton.widget.RecordProgressButton;

public class MainActivity extends AppCompatActivity {
    private  String TAG=this.getClass().getSimpleName();
    private RecordProgressButton mRecordButton;
    private Handler MainHandler=new Handler(Looper.getMainLooper());
    private MyThread myThread;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecordButton=findViewById(R.id.ProgressButton);
        //刻度最大值
        mRecordButton.setMaxProgress(600);
        mRecordButton.setOnRecordListener(new RecordProgressButton.RecordListener(){
            @Override
            public void onStart() {
                Log.e(TAG,"setOnRecordListener");
                myThread =new MyThread(mRecordButton);
                if (!myThread.isAlive()){
                    myThread.start();
                    Log.e(TAG," myThread.start()");
                }

            }

            @Override
            public void onEnd() {
                if (myThread!=null){
                    myThread.isRun=false;
                    myThread=null;
                    Log.e(TAG," myThread.stop");
                }

            }
        });

    }
    private class  MyThread extends Thread implements Runnable{
        private RecordProgressButton mainRecordButton;
        private boolean isRun=true;
        private int currentProgress;
        public MyThread(RecordProgressButton RecordButton) {
            mainRecordButton=RecordButton;
        }

        @Override
        public void run() {
            super.run();
            while (isRun){
                Log.e(TAG," myThread.isRun");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                currentProgress+=12;
                MainHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mainRecordButton.setCurrentProgress((int) currentProgress);
                    }
                });
            }
        }

    }
}
